package com.yanbao.controller;

import com.free.common.vo.ResultModel;
import com.free.exception.SystemException;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.yanbao.properties.ConstantsProperties;
import com.yanbao.service.FileSaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * <p>
 * 功能概述:
 * </p>
 * <p>
 * 功能详述:
 * </p>
 */
@RequestMapping(value = "/admin")
@Controller
public class AdminController {
    /*========================================================================*
     *                         Public Fields (公共属性)
     *========================================================================*/

    /*========================================================================*
     *                         Private Fields (私有属性)
     *========================================================================*/

    @Autowired
    private ConstantsProperties properties;

    @Autowired
    private FileSaveService service;
    /*========================================================================*
     *                         Construct Methods (构造方法)
     *========================================================================*/

    /*========================================================================*
     *                         Public Methods (公有方法)
     *========================================================================*/

    @RequestMapping(value = "/index")
    public ModelAndView index(HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/index");
        //mv.addObject("","")
        return mv;
    }

    @RequestMapping(value = "/imgs")
    public ModelAndView managerList(HttpServletRequest request,Integer type){
        ModelAndView mv = new ModelAndView("/imgs");
        mv.addObject("pic","1");
        if(type == null || type == 1){
            type = 1;
            mv.addObject("typeFlg",1);
            mv.addObject("type",type);
            mv.addObject("domain",properties.getPcdomain());
            mv.addObject("viewName","电脑端");
        }else{
            type = 2;
            mv.addObject("typeFlg",2);
            mv.addObject("type",type);
            mv.addObject("domain",properties.getMdomain());
            mv.addObject("viewName","手机端");
        }

        mv.addObject("imgs",service.getImgList(type));
        return mv;
    }

    @RequestMapping(value = "/imgupload")
    @ResponseBody
    public ResultModel imgUpload(MultipartFile myFile, String name, Integer type, Integer idx) throws IOException, SystemException {
        return service.uploadFile(myFile.getBytes(), name, type, idx);
    }


    /*========================================================================*
     *                         Private Methods (私有方法)
     *========================================================================*/
}
